package Partition;

/**
 * Created with Intellij IDEA.
 * Description;
 * User:TQ02
 * Data:2024-02-03
 * Time:18:48
 * Text：75. 颜色分类
 */
public class Text1 {
    public static void swap(int[]nums,int left,int right){
        int swap=nums[left];
        nums[left]=nums[right];
        nums[right]=swap;
    }
    public static void sortColors(int[] nums) {
        int left=0,right=nums.length-1,mid=0;
        while(mid<=right){
            while((nums[mid]==2 || nums[mid]==0) && mid<=right && mid>=left){
                if(nums[mid]==0){
                    swap(nums,mid,left);
                    left++;
                }if(nums[mid]==2){
                    swap(nums,mid,right);
                    right--;
                }
            }
            mid++;
        }
    }
    //代码优化
    public static void sortColors2(int[] nums) {
        int left=-1,right=nums.length,mid=0;
        while(mid<right){
            if(nums[mid]==0) swap(nums,++left,mid++);
            else if(nums[mid]==1) mid++;
            else swap(nums,--right,mid);
        }
    }
    public static void main(String[] args) {
        int[] array={2,1};
        sortColors(array);

    }
}
